package com.qdlc.p2p.manage.action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.MovieConfigService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.MovieConfig;
import com.qdlc.p2p.dal.model.MovieConfigModel;
import org.apache.struts2.convention.annotation.Action;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 电影票配置管理
 * 
 * @author ygw
 * @version 1.0
 * @since 2016年1月22日
 */
public class ManageMovieConfigAction extends BaseAction<MovieConfigModel> implements ModelDriven<MovieConfigModel> {

	@Resource
	private MovieConfigService movieConfigService;

	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * 列表页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigManager")
	public String movieConfigManager() throws Exception {
		return "movieConfigManager";
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigList")
	public void movieConfigList() throws Exception {
		PageDataList<MovieConfigModel> pageList = movieConfigService.list(model);
		int totalPage = pageList.getPage().getTotal(); // 总页数
		map.put("total", totalPage);
		map.put("rows", pageList.getList());
		printJson(getStringOfJpaObj(map));
	}

	/**
	 * 新增页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigAddPage")
	public String movieConfigAddPage() throws Exception {
		return "movieConfigAddPage";
	}
	/**
	 * 新增
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigAdd")
	public void movieConfigAdd() throws Exception {
		double price = model.getPrice();
		// 校验金额
		model.checkMoney(price);
		movieConfigService.add(model.prototype());
		printResult(MessageUtil.getMessage("I10001"), true);
	}

	/**
	 * 修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigEditPage")
	public String movieConfigEditPage() throws Exception {
		MovieConfig movieConfig = movieConfigService.find(model.getId());
		request.setAttribute("movieConfig", movieConfig);
		return "movieConfigEditPage";
	}

	/**
	 * 修改
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/movieConfig/movieConfigEdit")
	public void movieConfigEdit() throws Exception {
		int id = paramInt("id");
		double price = model.getPrice();
		// 校验金额
		model.checkMoney(price);
		MovieConfig movieConfig = movieConfigService.find(id);
		movieConfig.setName(model.getName());
		movieConfig.setPrice(price);
		movieConfig.setUpdateTime(DateUtil.getNow());
		movieConfigService.update(movieConfig);
		printResult(MessageUtil.getMessage("I10002"), true);
	}

}
